//
//  TCCalendarTipView.m
//  TCCalendarSelector
//
//  Created by 叮叮 on 2021/5/8.
//

#import "TCCalendarTipView.h"

@interface TCCalendarTipView()
@property (nonatomic) UIView *contentView;
@property (nonatomic, copy) NSArray *week;
@end

@implementation TCCalendarTipView

- (NSArray *)week {
    return @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    UIView *contentView = [[UIView alloc] initWithFrame:self.bounds];
    contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:effect];
    visualView.frame = contentView.bounds;
    [contentView addSubview:visualView];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.backgroundColor = UIColor.whiteColor;
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.textColor = UIColor.blackColor;
    textLabel.font = [UIFont boldSystemFontOfSize:20];
    textLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [contentView addSubview:textLabel];
    _textLabel = textLabel;
    
    NSLayoutConstraint *textLabelHeight = [NSLayoutConstraint constraintWithItem:textLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:self.bounds.size.height - 20];
    NSLayoutConstraint *textLabelCenterX = [NSLayoutConstraint constraintWithItem:textLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *textLabelTop = [NSLayoutConstraint constraintWithItem:textLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    [contentView addConstraint:textLabelHeight];
    [contentView addConstraint:textLabelCenterX];
    [contentView addConstraint:textLabelTop];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.backgroundColor = UIColor.whiteColor;
    cancelButton.translatesAutoresizingMaskIntoConstraints = NO;
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelButton];
    
    NSLayoutConstraint *cancelButtonLeft = [NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:15];
    NSLayoutConstraint *cancelButtonCenterY = [NSLayoutConstraint constraintWithItem:cancelButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    [contentView addConstraint:cancelButtonLeft];
    [contentView addConstraint:cancelButtonCenterY];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmButton.backgroundColor = UIColor.whiteColor;
    confirmButton.translatesAutoresizingMaskIntoConstraints = NO;
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:confirmButton];

    NSLayoutConstraint *confirmButtonRight = [NSLayoutConstraint constraintWithItem:confirmButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeRight multiplier:1 constant:-15];
    NSLayoutConstraint *confirmButtonCenterY = [NSLayoutConstraint constraintWithItem:confirmButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    [contentView addConstraint:confirmButtonRight];
    [contentView addConstraint:confirmButtonCenterY];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.backgroundColor = UIColor.whiteColor;
    leftButton.translatesAutoresizingMaskIntoConstraints = NO;
    [leftButton setTitle:@"←" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [leftButton addTarget:self action:@selector(leftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:leftButton];
    
    NSLayoutConstraint *leftButtonRight = [NSLayoutConstraint constraintWithItem:leftButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeLeft multiplier:1 constant:-5];
    NSLayoutConstraint *leftButtonCenterY = [NSLayoutConstraint constraintWithItem:leftButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    [contentView addConstraint:leftButtonRight];
    [contentView addConstraint:leftButtonCenterY];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightButton.backgroundColor = UIColor.whiteColor;
    rightButton.translatesAutoresizingMaskIntoConstraints = NO;
    [rightButton setTitle:@"→" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [rightButton addTarget:self action:@selector(rightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:rightButton];
    
    NSLayoutConstraint *rightButtonLeft = [NSLayoutConstraint constraintWithItem:rightButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeRight multiplier:1 constant:5];
    NSLayoutConstraint *rightButtonCenterY = [NSLayoutConstraint constraintWithItem:rightButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:textLabel attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    [contentView addConstraint:rightButtonLeft];
    [contentView addConstraint:rightButtonCenterY];
    
    
    CGFloat separatorLineHeight = 1 / UIScreen.mainScreen.scale;
    
    UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - separatorLineHeight, self.bounds.size.width, separatorLineHeight)];
    separatorLine.backgroundColor = UIColor.systemGroupedBackgroundColor;
    [contentView addSubview:separatorLine];
    
    /// 创建星期一到星期日视图
    for (int i = 0; i < self.week.count; i++) {
        NSString *weekDay = self.week[i];
        UILabel *weekLabel = [[UILabel alloc] init];
        weekLabel.backgroundColor = UIColor.whiteColor;
        weekLabel.textAlignment = NSTextAlignmentCenter;
        weekLabel.font = [UIFont systemFontOfSize:10];
        weekLabel.frame = CGRectMake(self.bounds.size.width / 7 * i, self.bounds.size.height - 20, self.bounds.size.width / 7, 20);
        weekLabel.text = weekDay;
        [contentView addSubview:weekLabel];
    }
}

- (void)cancel {
    if (_delegate && [_delegate respondsToSelector:@selector(cancelButtonClicked)]) {
        [_delegate cancelButtonClicked];
    }
}

- (void)confirm {
    if (_delegate && [_delegate respondsToSelector:@selector(confirmButtonClicked)]) {
        [_delegate confirmButtonClicked];
    }
}

- (void)leftButtonClicked:(UIButton *)button {
    if (_delegate && [_delegate respondsToSelector:@selector(previousButtonClicked:textLabel:)]) {
        [_delegate previousButtonClicked:button textLabel:self.textLabel];
    }
}

- (void)rightButtonClicked:(UIButton *)button {
    if (_delegate && [_delegate respondsToSelector:@selector(nextButtonClicked:textLabel:)]) {
        [_delegate nextButtonClicked:button textLabel:self.textLabel];
    }
}

@end
